package DBLayer;
import ModelLayer.*;

import java.sql.*;
import java.util.ArrayList;

public class DBLager implements IFDBLager {
	private Connection con;
	private PreparedStatement pStmtSelect;
	
	public DBLager()
	{
		con = DBConnection.getInstance().getDBcon();
		pStmtSelect = null;
	}
	
	
	public Lager findWithPreQuery(int lagerNr, boolean retriveAssociation)
    {   System.out.println("Find with preCompiled");
        if(pStmtSelect == null){
           pStmtSelect =  getPreparedStmt();          
        }
        Lager lagerObj = null;
        ResultSet results;
        try{
       
        pStmtSelect.setInt(1,lagerNr);
        results = pStmtSelect.executeQuery();
        if(results.next()){
             lagerObj = buildLager(results);	
        }
        
        }
        catch(Exception e){
            e.getMessage();
        }
        return lagerObj;
    }
	
	private PreparedStatement getPreparedStmt()
    {
       try{ 
        String query = "select * from Lager" +
                       " where lager = ?";
        System.out.println("Preparestatement " + query);
        pStmtSelect = con.prepareStatement(query);
        
       }
       catch(Exception e){
            e.getMessage();
        }
       return pStmtSelect;
    }


	
	private Lager singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		Lager lObj = new Lager();
                
	        String query =  buildQuery(wClause);
                System.out.println(query);
		try{ // read the salgsvare from the database
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() )
	 		{
	 			lObj = buildLager(results);
	 			
                            //assocaition is to be build
	 			String query1 = buildQuery2(wClause);
	 			stmt = con.createStatement();
		 		stmt.setQueryTimeout(5);
		 		results = stmt.executeQuery(query1);
		 		System.out.println(query1);
		 		while(results.next()){
		 			LagerVare LvObj = buildDetail(results, lObj);
		 			lObj.setLV(LvObj);
		 		}
                            stmt.close();		
			}               
		//end try
		}
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return lObj;
	}
	private LagerVare buildDetail(ResultSet results, Lager lObj) {
		// TODO Auto-generated method stub
		return null;
	}


	//method to build the query
	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM Lager";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}
	private String buildQuery2(String wClause)
	{
	    String query="SELECT * FROM LagerVare";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}
	
	
	
	//method to build an SaleLineItem object
	private Lager buildLager(ResultSet results)
      {   Lager lObj = new Lager();
          try{ // the columns from the table Lager are used
        	  
                lObj.setLagerNr(results.getInt("lagerNr"));
                lObj.setPlacering(results.getString("placering"));

                }
         catch(Exception e)
         {
             System.out.println("error in building the lager object");
         }
         return lObj;
      }
	

	
	public Lager findLagerNr(int lagerNr, boolean retriveAssociation)
	{
		Lager lObj = new Lager();
		lObj = findWithPreQuery(lagerNr, retriveAssociation);
		return lObj;
	}
	public Lager findLagerNr1(int lagerNr, boolean retriveAssociation)
	{
		Lager lObj = new Lager();
		String Wclause = "LagerNr ='"+ lagerNr + "'";
		lObj = singleWhere(Wclause, retriveAssociation);
		return lObj;
	}
	
	public ArrayList<SalgsVare> getAllSalgsVare(boolean retriveAssociation) {
		// TODO Auto-generated method stub
		return null;
	}

	
}